window.GoogleUtil = {};
//测距变量
var polyline;
var polylinesArray = [];
//距离标记数组
var lenArray = [];
var rulerActions = [];
var startMarker = null;

GoogleUtil.control = {
	/**
	 * 测距控件
	 */
	addRulerControl: function () {
		var RulerControl = function (rulerControlDiv, map) {


			//启动整个地图的click侦听
			var rule = google.maps.event.addListener(map, "click", function (event) {
				var prePos = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
				if (startMarker == null) {
					var marker = new MarkerWithLabel({
						position: prePos,
						map: map,
						labelContent: "",
						labelClass: "labels1",
						labelStyle: {opacity: 1},
						icon: path + "/map/images/track/start.gif"
					});
					startMarker = marker;
					//将标记压入数组
					lenArray.push(marker);
				} else {
					var marker1 = new MarkerWithLabel({
						position: prePos,
						map: map,
						labelContent: "",
						labelClass: "labels1",
						labelStyle: {opacity: 1},
						icon: path + "/map/images/toolbar/point.png"
					});
					//将标记压入数组
					lenArray.push(marker1);
					//计算距离
					var distance = GoogleUtil.control.drawOverlay();
					marker1.set("labelContent", distance);
				}

			});
			rulerActions.push(rule);

		};


		var rulerControlDiv = document.createElement('div');
		RulerControl(rulerControlDiv, map);
		rulerControlDiv.index = 1;
		map.controls[google.maps.ControlPosition.LEFT_TOP].push(rulerControlDiv);
	},
	/**
	 * 清除测距
	 */
	addClearRulerOverlayControl: function () {
		var RulerControl = function (rulerControlDiv, map) {
			startMarker = null;
			while (rulerActions[0]) {
				google.maps.event.removeListener(rulerActions.pop());
			}
			while (lenArray[0]) {
				lenArray.pop().setMap(null);
			}
			if (polylinesArray) {
				for (i in polylinesArray) {
					if (typeof (polylinesArray[i].setMap) != "undefined") {
						polylinesArray[i].setMap(null);
					}

				}
				polylinesArray = [];
			}

		};


		var rulerControlDiv = document.createElement('div');
		RulerControl(rulerControlDiv, map);
		rulerControlDiv.index = 1;
		map.controls[google.maps.ControlPosition.LEFT_TOP].push(rulerControlDiv);
	},
	/**
	 * 测距绘制
	 */
	drawOverlay: function () {
		//路线数组
		var flightPlanCoordinates = [];
		//将坐标压入路线数组
		if (lenArray) {
			for (var i in lenArray) {
				if (typeof (lenArray[i].position) != "undefined") {
					flightPlanCoordinates.push(lenArray[i].position);
				}

			}
		}
		//路径选项
		var polylineOptions = {
			path: flightPlanCoordinates,
			map: map,
			strokeColor: "#FC7F43",
			strokeOpacity: 1.0,
			strokeWeight: 2
		};
		polyline = new google.maps.Polyline(polylineOptions);
		polyline.setMap(map);
		polylinesArray.push(polyline);
		return ((polyline.getLength() / 1000).toFixed(3) + "公里");
	}

};

//google 测距
google.maps.LatLng.prototype.distanceFrom = function (latlng) {
	var lat = [this.lat(), latlng.lat()];
	var lng = [this.lng(), latlng.lng()];
	var R = 6378137;
	var dLat = (lat[1] - lat[0]) * Math.PI / 180;
	var dLng = (lng[1] - lng[0]) * Math.PI / 180;
	var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat[0] * Math.PI / 180) * Math.cos(lat[1] * Math.PI / 180) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
	var d = R * c;
	return Math.round(d);
};


google.maps.Marker.prototype.distanceFrom = function (marker) {
	return this.getPosition().distanceFrom(marker.getPosition());
};


google.maps.Polyline.prototype.getLength = function () {
	var d = 0;
	var path = this.getPath();
	var latlng;
	for (var i = 0; i < path.getLength() - 1; i++) {
		latlng = [path.getAt(i), path.getAt(i + 1)];
		d += latlng[0].distanceFrom(latlng[1]);
	}
	return d;
};